/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-04-06
 * Time: 19:07
 */
public class Test {
    public boolean hasCycle(MySingleList.ListNode head) {
        MySingleList.ListNode fast = head;
        MySingleList.ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }
    public MySingleList.ListNode getIntersectionNode(MySingleList.ListNode headA, MySingleList.ListNode headB) {
        //1.
        int len1 = 0;
        int len2 = 0;
        MySingleList.ListNode plong = headA;
        MySingleList.ListNode pshort = headB;
        while(plong != null) {
            len1++;
            plong = plong.next;
        }
        while(pshort != null) {
            len2++;
            pshort = pshort.next;
        }
        int x = len1 - len2;
        if(x < 0) {
            pshort = headA;
            x = -x;
        } else {
            pshort = headB;
        }
        while(x != 0) {
            plong = plong.next;
            x--;
        }
        while(pshort != plong) {
            plong = plong.next;
            pshort = pshort.next;
        }
        return pshort;

    }
    public static MySingleList.ListNode mergeTwoLists(MySingleList.ListNode list1, MySingleList.ListNode list2) {
        MySingleList.ListNode newHead = new MySingleList.ListNode(-1);
        MySingleList.ListNode tmpH = newHead;
        while(list1 != null && list2 != null) {
            if(list1.val < list2.val) {
                tmpH.next = list1;
                tmpH = tmpH.next;
                list1 = list1.next;
            } else {
                tmpH.next = list2;
                tmpH = tmpH.next;
                list2 = list2.next;
            }
        }
        if(list1 == null) {
            tmpH.next = list2;
        } else {
            tmpH.next = list1;
        }
        return newHead.next;
    }
    public static void main(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addLast(12);
        mySingleList1.addLast(23);
        mySingleList1.addLast(55);
        mySingleList1.addLast(61);
        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(22);
        mySingleList2.addLast(25);
        mySingleList2.addLast(43);
        mySingleList2.addLast(52);
        MySingleList.ListNode ret = mergeTwoLists(mySingleList1.head, mySingleList2.head);
        mySingleList1.show(ret);
//        mySingleList.createList();
//        mySingleList.addFirst(12);
//        mySingleList.addFirst(12);
//        mySingleList.addFirst(12);
//        mySingleList.addFirst(12);
//        mySingleList.addFirst(12);
//        mySingleList.show();
//        mySingleList.reverse();
//        mySingleList.clear();
//        mySingleList.show();
//        mySingleList.show();
//        int n = mySingleList.size();
//        System.out.println(n);
//        boolean ret = mySingleList.contains(11);
//        System.out.println(ret);
//        mySingleList.addFirst(99);
//        mySingleList.show();
//        MySingleList mySingleList1 = new MySingleList();
//        mySingleList1.addLast(66);
//        mySingleList1.show();
//        mySingleList.add(66,2);
//        mySingleList.show();
//        mySingleList1.add(1,0);
//        mySingleList1.show();
//        mySingleList.removeAllKey(12);
//        mySingleList.show();
    }
}
